{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nclass: 3862\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "the_input (InputLayer)       (None, 32, 100, 1)        0         \n",
      "_________________________________________________________________\n",
      "conv1 (Conv2D)               (None, 32, 100, 64)       640       \n",
      "_________________________________________________________________\n",
      "pool1 (MaxPooling2D)         (None, 16, 50, 64)        0         \n",
      "_________________________________________________________________\n",
      "conv2 (Conv2D)               (None, 16, 50, 128)       73856     \n",
      "_________________________________________________________________\n",
      "pool2 (MaxPooling2D)         (None, 8, 25, 128)        0         \n",
      "_________________________________________________________________\n",
      "conv3 (Conv2D)               (None, 8, 25, 256)        295168    \n",
      "_________________________________________________________________\n",
      "batch_normalization_4 (Batch (None, 8, 25, 256)        1024      \n",
      "_________________________________________________________________\n",
      "conv4 (Conv2D)               (None, 8, 25, 256)        590080    \n",
      "_________________________________________________________________\n",
      "zero_padding2d_3 (ZeroPaddin (None, 8, 27, 256)        0         \n",
      "_________________________________________________________________\n",
      "pool3 (MaxPooling2D)         (None, 4, 26, 256)        0         \n",
      "_________________________________________________________________\n",
      "conv5 (Conv2D)               (None, 4, 26, 512)        1180160   \n",
      "_________________________________________________________________\n",
      "batch_normalization_5 (Batch (None, 4, 26, 512)        2048      \n",
      "_________________________________________________________________\n",
      "conv6 (Conv2D)               (None, 4, 26, 512)        2359808   \n",
      "_________________________________________________________________\n",
      "zero_padding2d_4 (ZeroPaddin (None, 4, 28, 512)        0         \n",
      "_________________________________________________________________\n",
      "pool4 (MaxPooling2D)         (None, 2, 27, 512)        0         \n",
      "_________________________________________________________________\n",
      "conv7 (Conv2D)               (None, 1, 26, 512)        1049088   \n",
      "_________________________________________________________________\n",
      "batch_normalization_6 (Batch (None, 1, 26, 512)        2048      \n",
      "_________________________________________________________________\n",
      "permute (Permute)            (None, 26, 1, 512)        0         \n",
      "_________________________________________________________________\n",
      "timedistrib (TimeDistributed (None, 26, 512)           0         \n",
      "_________________________________________________________________\n",
      "blstm1 (Bidirectional)       (None, 26, 512)           1181184   \n",
      "_________________________________________________________________\n",
      "blstm2 (Bidirectional)       (None, 26, 512)           1181184   \n",
      "_________________________________________________________________\n",
      "blstm2_out (Dense)           (None, 26, 3862)          1981206   \n",
      "=================================================================\n",
      "Total params: 9,897,494\n",
      "Trainable params: 9,894,934\n",
      "Non-trainable params: 2,560\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "#CRNN\n",
    "#Edit:2017-09-14 ~ 2017-09-17\n",
    "#@sima\n",
    "#%%\n",
    "from keras.layers.convolutional import Conv2D,MaxPooling2D,ZeroPadding2D\n",
    "from keras.layers.normalization import BatchNormalization\n",
    "from keras.layers.core import Reshape,Masking,Lambda,Permute\n",
    "from keras.layers import Input,Dense,Flatten\n",
    "from keras.preprocessing.sequence import pad_sequences\n",
    "from keras.layers.recurrent import GRU,LSTM\n",
    "from keras.layers.wrappers import Bidirectional\n",
    "from keras.models import Model\n",
    "from keras import backend as K\n",
    "from keras.preprocessing import image\n",
    "from keras.optimizers import Adam,SGD,Adadelta\n",
    "from keras import losses\n",
    "from keras.layers.wrappers import TimeDistributed\n",
    "from keras.callbacks import EarlyStopping,ModelCheckpoint,TensorBoard\n",
    "from keras.utils import plot_model\n",
    "from matplotlib import pyplot as plt\n",
    "\n",
    "import numpy as np \n",
    "import os\n",
    "from PIL import Image \n",
    "import json\n",
    "import threading\n",
    "\n",
    "import tensorflow as tf  \n",
    "import keras.backend.tensorflow_backend as K  \n",
    "\n",
    "def get_session(gpu_fraction=0.6):  \n",
    "    '''''Assume that you have 6GB of GPU memory and want to allocate ~2GB'''  \n",
    "  \n",
    "    num_threads = os.environ.get('OMP_NUM_THREADS')  \n",
    "    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=gpu_fraction)  \n",
    "  \n",
    "    if num_threads:  \n",
    "        return tf.Session(config=tf.ConfigProto(  \n",
    "            gpu_options=gpu_options, intra_op_parallelism_threads=num_threads))  \n",
    "    else:  \n",
    "        return tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))  \n",
    "  \n",
    "K.set_session(get_session()) \n",
    "\n",
    "\n",
    "\n",
    "def ctc_lambda_func(args):\n",
    "    y_pred,labels,input_length,label_length = args\n",
    "    return K.ctc_batch_cost(labels, y_pred, input_length, label_length)\n",
    "\n",
    "\n",
    "char=''\n",
    "with open('E:\\deeplearn\\OCR\\chi_sim\\随机语料\\yu\\char.txt',encoding='utf-8') as f:\n",
    "      for ch in f.readlines():\n",
    "            ch = ch.strip('\\r\\n')\n",
    "            char=char+ch\n",
    "char =char+'^'\n",
    "print('nclass:',len(char))\n",
    "\n",
    "char_to_id = {j:i for i,j in enumerate(char)}\n",
    "id_to_char = {i:j for i,j in enumerate(char)}\n",
    "\n",
    "maxlabellength = 20\n",
    "img_h = 32\n",
    "img_w = 248\n",
    "nclass = len(char)\n",
    "rnnunit=256\n",
    "batch_size =64\n",
    "#gen = image.ImageDataGenerator(rescale=1.0/255)\n",
    "\n",
    "\n",
    "def gen1(jsonpath,batchsize=64,maxlabellength=8,imagesize=(32,230)):\n",
    "    with open(jsonpath,'r',encoding='utf-8') as f:\n",
    "        image_label = json.load(f)\n",
    "    imagepathlist = [i for i,_ in image_label]\n",
    "    imagepathlist = np.array(imagepathlist)\n",
    "    while 1:\n",
    "       \n",
    "       labels = np.ones([batchsize,maxlabellength])\n",
    "       input_length = np.zeros([batchsize,1])\n",
    "       label_length = np.zeros([batchsize,1])\n",
    "\n",
    "\n",
    "def gen2(jsonpath,imagepath,batchsize=64,maxlabellength=8,imagesize=(32,248)):\n",
    "    with open(jsonpath,'r',encoding='utf-8') as f:\n",
    "        image_label = json.load(f)\n",
    "\n",
    "    print('open json')\n",
    "    imagelabel =[i['label'] for _,i in image_label.items()]\n",
    "    _imagefile = [i for i,j in image_label.items()]\n",
    "    print('--begin gen2')\n",
    "    v = gen.flow_from_directory(imagepath,target_size=imagesize,\n",
    "                            color_mode='grayscale',class_mode='sparse',shuffle=True,\n",
    "                            #save_to_dir=r'E:\\deeplearn\\OCR\\Sample\\fixsizetrain',\n",
    "                            batch_size = batchsize\n",
    "                            )\n",
    "\n",
    "    v.classes = np.array([i for i in range(len(imagelabel))])\n",
    "    v.filenames = _imagefile\n",
    "    print('end gen2')\n",
    "    \n",
    "    while 1:\n",
    "       x,l = next(v)\n",
    "       bz = len(l)\n",
    "       labels = np.ones([bz,maxlabellength])\n",
    "       input_length = np.zeros([bz,1])\n",
    "       label_length = np.zeros([bz,1])\n",
    "       for i in range(bz):\n",
    "           str = imagelabel[l[i]]\n",
    "           label_length[i] = len(str)        \n",
    "\n",
    "           input_length[i] = imagesize[1]//4+1\n",
    "           labels[i,:len(str)] =[char_to_id[i] for i in str]\n",
    "#            print(str)\n",
    "#        print(labels)\n",
    "#        print(_imagefile[l[i]])\n",
    "#        print(label_length)\n",
    "#        print(input_length)\n",
    "\n",
    "       inputs = {'the_input': x,\n",
    "                'the_labels': labels,\n",
    "                'input_length': input_length,\n",
    "                'label_length': label_length,\n",
    "                }\n",
    "       outputs = {'ctc': np.zeros([batchsize])} \n",
    "       #output = [x,labels,input_length,label_length]\n",
    "       yield (inputs,outputs)\n",
    "        \n",
    "class random_uniform_num():\n",
    "    \"\"\"\n",
    "    均匀随机，确保每轮每个只出现一次\n",
    "    \"\"\"\n",
    "    def __init__(self,total):\n",
    "        self.total = total\n",
    "        self.range = [i for i in range(total)]\n",
    "        np.random.shuffle(self.range)\n",
    "        self.index = 0\n",
    "    def get(self,batchsize):\n",
    "        r_n=[]\n",
    "        if(self.index+batchsize>self.total):\n",
    "            r_n_1 = self.range[self.index:self.total]\n",
    "            np.random.shuffle(self.range)\n",
    "            self.index = (self.index+batchsize)-self.total\n",
    "            r_n_2 = self.range[0:self.index]\n",
    "            r_n.extend(r_n_1)\n",
    "            r_n.extend(r_n_2)\n",
    "            \n",
    "        else:\n",
    "            r_n = self.range[self.index:self.index+batchsize]\n",
    "            self.index = self.index+batchsize\n",
    "        return r_n    \n",
    "    \n",
    "def gen3(jsonpath,imagepath,batchsize=64,maxlabellength=8,imagesize=(32,356)):\n",
    "    with open(jsonpath,'r',encoding='utf-8') as f:\n",
    "        image_label = json.load(f)\n",
    "\n",
    "    print('open json')\n",
    "    #imagelabel =[i['label'] for _,i in image_label.items()]\n",
    "    _imagefile = [i for i,j in image_label.items()]\n",
    "    x = np.zeros((batchsize, imagesize[0], imagesize[1], 1), dtype=np.float)\n",
    "    labels = np.ones([batchsize,maxlabellength])*10000\n",
    "    input_length = np.zeros([batchsize,1])\n",
    "    label_length = np.zeros([batchsize,1])\n",
    "    \n",
    "    r_n = random_uniform_num(len(_imagefile))\n",
    "    print('图片总量',len(_imagefile))\n",
    "    _imagefile = np.array(_imagefile)\n",
    "   \n",
    "    while 1:\n",
    "       \n",
    "       shufimagefile = _imagefile[r_n.get(batchsize)]\n",
    "       for i,j in enumerate(shufimagefile):\n",
    "           img1 = Image.open(j)\n",
    "           img = np.array(img1,'f')/255.0-0.5\n",
    "           \n",
    "           x[i] = np.expand_dims(img,axis=2)\n",
    "           #print('imag:shape',img.shape)\n",
    "           str = image_label[j]['label']\n",
    "           label_length[i] = len(str)        \n",
    "           if(len(str)<=0):\n",
    "                print(\"len<0\",j)\n",
    "           input_length[i] = imagesize[1]//4+1\n",
    "           labels[i,:len(str)] =[char_to_id[i] for i in str]\n",
    "        \n",
    "#        print('-------------------------------')\n",
    "#        print('x shape:',x.shape)\n",
    "#        print(str)\n",
    "#        print('labes',labels)\n",
    "#        print('labellength:',label_length)\n",
    "#        print('inputlenght',input_length)\n",
    "#        print(x)\n",
    "\n",
    "       inputs = {'the_input': x,\n",
    "                'the_labels': labels,\n",
    "                'input_length': input_length,\n",
    "                'label_length': label_length,\n",
    "                }\n",
    "       outputs = {'ctc': np.zeros([batchsize])} \n",
    "       yield (inputs,outputs)\n",
    "        \n",
    "\n",
    "input = Input(shape=(img_h,None,1),name='the_input')\n",
    "m = Conv2D(64,kernel_size=(3,3),activation='relu',padding='same',name='conv1')(input)\n",
    "m = MaxPooling2D(pool_size=(2,2),strides=(2,2),name='pool1')(m)\n",
    "m = Conv2D(128,kernel_size=(3,3),activation='relu',padding='same',name='conv2')(m)\n",
    "m = MaxPooling2D(pool_size=(2,2),strides=(2,2),name='pool2')(m)\n",
    "m = Conv2D(256,kernel_size=(3,3),activation='relu',padding='same',name='conv3')(m)\n",
    "m = BatchNormalization(axis=3)(m)\n",
    "m = Conv2D(256,kernel_size=(3,3),activation='relu',padding='same',name='conv4')(m)\n",
    "\n",
    "m = ZeroPadding2D(padding=(0,1))(m)\n",
    "m = MaxPooling2D(pool_size=(2,2),strides=(2,1),padding='valid',name='pool3')(m)\n",
    "\n",
    "m = Conv2D(512,kernel_size=(3,3),activation='relu',padding='same',name='conv5')(m)\n",
    "m = BatchNormalization(axis=3)(m)\n",
    "m = Conv2D(512,kernel_size=(3,3),activation='relu',padding='same',name='conv6')(m)\n",
    "\n",
    "m = ZeroPadding2D(padding=(0,1))(m)\n",
    "m = MaxPooling2D(pool_size=(2,2),strides=(2,1),padding='valid',name='pool4')(m)\n",
    "m = Conv2D(512,kernel_size=(2,2),activation='relu',padding='valid',name='conv7')(m)\n",
    "m = BatchNormalization(axis=3)(m)\n",
    "\n",
    "m = Permute((2,1,3),name='permute')(m)\n",
    "m = TimeDistributed(Flatten(),name='timedistrib')(m)\n",
    "\n",
    "m = Bidirectional(GRU(rnnunit,return_sequences=True,implementation=2),name='blstm1')(m)\n",
    "#m = Bidirectional(LSTM(rnnunit,return_sequences=True),name='blstm1')(m)\n",
    "m = Dense(rnnunit,name='blstm1_out',activation='linear',)(m)\n",
    "#m = Bidirectional(LSTM(rnnunit,return_sequences=True),name='blstm2')(m)\n",
    "m = Bidirectional(GRU(rnnunit,return_sequences=True,implementation=2),name='blstm2')(m)\n",
    "y_pred = Dense(nclass,name='blstm2_out',activation='softmax')(m)\n",
    "\n",
    "basemodel = Model(inputs=input,outputs=y_pred)\n",
    "basemodel.summary()\n",
    "\n",
    "\n",
    "labels = Input(name='the_labels',shape=[maxlabellength],dtype='float32')\n",
    "input_length = Input(name='input_length', shape=[1], dtype='int64')\n",
    "label_length = Input(name='label_length', shape=[1], dtype='int64')\n",
    "\n",
    "loss_out = Lambda(ctc_lambda_func, output_shape=(1,), name='ctc')([y_pred, labels, input_length, label_length]) \n",
    "\n",
    "model = Model(inputs=[input, labels, input_length, label_length], outputs=loss_out)\n",
    "\n",
    "\n",
    "adam = Adam()\n",
    "#tf_adam = tf.train.AdamOptimizer ()\n",
    "#adadelta = Adadelta()\n",
    "model.compile(loss={'ctc': lambda y_true, y_pred: y_pred}, optimizer=adam,metrics=['accuracy'])\n",
    "\n",
    "\n",
    "checkpoint = ModelCheckpoint(r'E:\\deeplearn\\OCR\\Sample\\model\\weights-{epoch:02d}.hdf5',\n",
    "                           save_weights_only=True)\n",
    "earlystop = EarlyStopping(patience=10)\n",
    "tensorboard = TensorBoard(r'E:\\deeplearn\\OCR\\Sample\\model\\tflog',write_graph=True)\n",
    "                           "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-----------beginfit--\n"
     ]
    }
   ],
   "source": [
    "\n",
    "\n",
    "print('-----------beginfit--')\n",
    "cc1=gen3(r'E:\\deeplearn\\OCR\\Sample\\trainnew\\trainlabel.json',r'E:\\deeplearn\\OCR\\Sample\\trainnew',batchsize=batch_size,maxlabellength=maxlabellength,imagesize=(img_h,img_w))\n",
    "cc2=gen3(r'E:\\deeplearn\\OCR\\Sample\\validnew\\validlabel.json',r'E:\\deeplearn\\OCR\\Sample\\validnew',batchsize=batch_size,maxlabellength=maxlabellength,imagesize=(img_h,img_w))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/100\n",
      "open json\n",
      "图片总量 145750\n",
      "2276/2277 [============================>.] - ETA: 0s - loss: 0.2515 - acc: 0.9315open json\n",
      "图片总量 1003\n",
      "2277/2277 [==============================] - 1188s - loss: 0.2515 - acc: 0.9315 - val_loss: 0.6258 - val_acc: 0.9292\n",
      "Epoch 2/100\n",
      "2277/2277 [==============================] - 1197s - loss: 0.1884 - acc: 0.9425 - val_loss: 0.4036 - val_acc: 0.9437\n",
      "Epoch 3/100\n",
      "2277/2277 [==============================] - 1187s - loss: 0.1764 - acc: 0.9450 - val_loss: 0.5295 - val_acc: 0.9333\n",
      "Epoch 4/100\n",
      "2277/2277 [==============================] - 1189s - loss: 0.1387 - acc: 0.9548 - val_loss: 0.5246 - val_acc: 0.9083\n",
      "Epoch 5/100\n",
      "2277/2277 [==============================] - 1193s - loss: 0.1543 - acc: 0.9502 - val_loss: 0.5483 - val_acc: 0.9458\n",
      "Epoch 6/100\n",
      "2277/2277 [==============================] - 1198s - loss: 0.1156 - acc: 0.9616 - val_loss: 0.4577 - val_acc: 0.9354\n",
      "Epoch 7/100\n",
      "2277/2277 [==============================] - 1187s - loss: 0.1364 - acc: 0.9550 - val_loss: 0.6575 - val_acc: 0.9229\n",
      "Epoch 8/100\n",
      "2277/2277 [==============================] - 1194s - loss: 0.1141 - acc: 0.9615 - val_loss: 0.8387 - val_acc: 0.8594\n",
      "Epoch 9/100\n",
      "2277/2277 [==============================] - 1185s - loss: 0.1216 - acc: 0.9594 - val_loss: 0.4584 - val_acc: 0.9490\n",
      "Epoch 10/100\n",
      "2277/2277 [==============================] - 1187s - loss: 0.1011 - acc: 0.9653 - val_loss: 0.3829 - val_acc: 0.9594\n",
      "Epoch 11/100\n",
      "2277/2277 [==============================] - 1187s - loss: 0.1115 - acc: 0.9626 - val_loss: 0.5715 - val_acc: 0.8979\n",
      "Epoch 12/100\n",
      "2277/2277 [==============================] - 1188s - loss: 0.1378 - acc: 0.9558 - val_loss: 0.6168 - val_acc: 0.9552\n",
      "Epoch 13/100\n",
      "2277/2277 [==============================] - 1183s - loss: 0.1121 - acc: 0.9626 - val_loss: 0.4110 - val_acc: 0.9500\n",
      "Epoch 14/100\n",
      "2277/2277 [==============================] - 1186s - loss: 0.0824 - acc: 0.9717 - val_loss: 0.4550 - val_acc: 0.9583\n",
      "Epoch 15/100\n",
      "2277/2277 [==============================] - 1186s - loss: 0.1113 - acc: 0.9629 - val_loss: 0.4641 - val_acc: 0.9437\n",
      "Epoch 16/100\n",
      "2277/2277 [==============================] - 1187s - loss: 0.2913 - acc: 0.9335 - val_loss: 0.7530 - val_acc: 0.9229\n",
      "Epoch 17/100\n",
      "2277/2277 [==============================] - 1187s - loss: 0.1166 - acc: 0.9612 - val_loss: 0.6686 - val_acc: 0.9542\n",
      "Epoch 18/100\n",
      "2277/2277 [==============================] - 1187s - loss: 0.0572 - acc: 0.9795 - val_loss: 0.3975 - val_acc: 0.9563\n",
      "Epoch 19/100\n",
      "2277/2277 [==============================] - 1187s - loss: 0.0631 - acc: 0.9779 - val_loss: 0.5708 - val_acc: 0.9458\n",
      "Epoch 20/100\n",
      "2277/2277 [==============================] - 1184s - loss: 0.1136 - acc: 0.9641 - val_loss: 0.6579 - val_acc: 0.9271\n",
      "Epoch 21/100\n",
      "2277/2277 [==============================] - 1187s - loss: 0.1346 - acc: 0.9578 - val_loss: 0.3697 - val_acc: 0.9646\n",
      "Epoch 22/100\n",
      "2277/2277 [==============================] - 1188s - loss: 0.0750 - acc: 0.9744 - val_loss: 0.3443 - val_acc: 0.9563\n",
      "Epoch 23/100\n",
      "2277/2277 [==============================] - 1183s - loss: 0.0953 - acc: 0.9685 - val_loss: 0.5711 - val_acc: 0.9333\n",
      "Epoch 24/100\n",
      "2277/2277 [==============================] - 1187s - loss: 0.0950 - acc: 0.9692 - val_loss: 0.5580 - val_acc: 0.9323\n",
      "Epoch 25/100\n",
      "2277/2277 [==============================] - 1187s - loss: 0.0953 - acc: 0.9686 - val_loss: 0.5170 - val_acc: 0.9365\n",
      "Epoch 26/100\n",
      "2277/2277 [==============================] - 1183s - loss: 0.0895 - acc: 0.9701 - val_loss: 0.6247 - val_acc: 0.9573\n",
      "Epoch 27/100\n",
      "2277/2277 [==============================] - 1187s - loss: 0.0924 - acc: 0.9696 - val_loss: 0.5905 - val_acc: 0.9521\n",
      "Epoch 28/100\n",
      "1472/2277 [==================>...........] - ETA: 1465s - loss: 0.1034 - acc: 0.9671"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-4-1a7c7704ba27>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      7\u001b[0m                     \u001b[0mvalidation_steps\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m1003\u001b[0m \u001b[1;33m//\u001b[0m \u001b[0mbatch_size\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m      8\u001b[0m                     \u001b[0mcallbacks\u001b[0m \u001b[1;33m=\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mearlystop\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mcheckpoint\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mtensorboard\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m                     \u001b[0mverbose\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     10\u001b[0m                     )\n",
      "\u001b[0;32mC:\\Users\\Admin\\Anaconda3\\lib\\site-packages\\keras\\legacy\\interfaces.py\u001b[0m in \u001b[0;36mwrapper\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m     85\u001b[0m                 warnings.warn('Update your `' + object_name +\n\u001b[1;32m     86\u001b[0m                               '` call to the Keras 2 API: ' + signature, stacklevel=2)\n\u001b[0;32m---> 87\u001b[0;31m             \u001b[1;32mreturn\u001b[0m \u001b[0mfunc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     88\u001b[0m         \u001b[0mwrapper\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_original_function\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mfunc\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m     89\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mwrapper\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\Users\\Admin\\Anaconda3\\lib\\site-packages\\keras\\engine\\training.py\u001b[0m in \u001b[0;36mfit_generator\u001b[0;34m(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)\u001b[0m\n\u001b[1;32m   2040\u001b[0m                     outs = self.train_on_batch(x, y,\n\u001b[1;32m   2041\u001b[0m                                                \u001b[0msample_weight\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0msample_weight\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2042\u001b[0;31m                                                class_weight=class_weight)\n\u001b[0m\u001b[1;32m   2043\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m   2044\u001b[0m                     \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0misinstance\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mouts\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlist\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\Users\\Admin\\Anaconda3\\lib\\site-packages\\keras\\engine\\training.py\u001b[0m in \u001b[0;36mtrain_on_batch\u001b[0;34m(self, x, y, sample_weight, class_weight)\u001b[0m\n\u001b[1;32m   1760\u001b[0m             \u001b[0mins\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mx\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0my\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0msample_weights\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m   1761\u001b[0m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_make_train_function\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1762\u001b[0;31m         \u001b[0moutputs\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtrain_function\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mins\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1763\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0moutputs\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m   1764\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[0moutputs\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\Users\\Admin\\Anaconda3\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, inputs)\u001b[0m\n\u001b[1;32m   2271\u001b[0m         updated = session.run(self.outputs + [self.updates_op],\n\u001b[1;32m   2272\u001b[0m                               \u001b[0mfeed_dict\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mfeed_dict\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2273\u001b[0;31m                               **self.session_kwargs)\n\u001b[0m\u001b[1;32m   2274\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mupdated\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m:\u001b[0m\u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0moutputs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m   2275\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\Users\\Admin\\Anaconda3\\lib\\site-packages\\tensorflow\\python\\client\\session.py\u001b[0m in \u001b[0;36mrun\u001b[0;34m(self, fetches, feed_dict, options, run_metadata)\u001b[0m\n\u001b[1;32m    893\u001b[0m     \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    894\u001b[0m       result = self._run(None, fetches, feed_dict, options_ptr,\n\u001b[0;32m--> 895\u001b[0;31m                          run_metadata_ptr)\n\u001b[0m\u001b[1;32m    896\u001b[0m       \u001b[1;32mif\u001b[0m \u001b[0mrun_metadata\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    897\u001b[0m         \u001b[0mproto_data\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtf_session\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mTF_GetBuffer\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mrun_metadata_ptr\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\Users\\Admin\\Anaconda3\\lib\\site-packages\\tensorflow\\python\\client\\session.py\u001b[0m in \u001b[0;36m_run\u001b[0;34m(self, handle, fetches, feed_dict, options, run_metadata)\u001b[0m\n\u001b[1;32m   1122\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mfinal_fetches\u001b[0m \u001b[1;32mor\u001b[0m \u001b[0mfinal_targets\u001b[0m \u001b[1;32mor\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mhandle\u001b[0m \u001b[1;32mand\u001b[0m \u001b[0mfeed_dict_tensor\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m   1123\u001b[0m       results = self._do_run(handle, final_targets, final_fetches,\n\u001b[0;32m-> 1124\u001b[0;31m                              feed_dict_tensor, options, run_metadata)\n\u001b[0m\u001b[1;32m   1125\u001b[0m     \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m   1126\u001b[0m       \u001b[0mresults\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\Users\\Admin\\Anaconda3\\lib\\site-packages\\tensorflow\\python\\client\\session.py\u001b[0m in \u001b[0;36m_do_run\u001b[0;34m(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)\u001b[0m\n\u001b[1;32m   1319\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mhandle\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m   1320\u001b[0m       return self._do_call(_run_fn, self._session, feeds, fetches, targets,\n\u001b[0;32m-> 1321\u001b[0;31m                            options, run_metadata)\n\u001b[0m\u001b[1;32m   1322\u001b[0m     \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m   1323\u001b[0m       \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_do_call\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0m_prun_fn\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_session\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mhandle\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfeeds\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfetches\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\Users\\Admin\\Anaconda3\\lib\\site-packages\\tensorflow\\python\\client\\session.py\u001b[0m in \u001b[0;36m_do_call\u001b[0;34m(self, fn, *args)\u001b[0m\n\u001b[1;32m   1325\u001b[0m   \u001b[1;32mdef\u001b[0m \u001b[0m_do_call\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfn\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m   1326\u001b[0m     \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1327\u001b[0;31m       \u001b[1;32mreturn\u001b[0m \u001b[0mfn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1328\u001b[0m     \u001b[1;32mexcept\u001b[0m \u001b[0merrors\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mOpError\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m   1329\u001b[0m       \u001b[0mmessage\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcompat\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mas_text\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0me\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmessage\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\Users\\Admin\\Anaconda3\\lib\\site-packages\\tensorflow\\python\\client\\session.py\u001b[0m in \u001b[0;36m_run_fn\u001b[0;34m(session, feed_dict, fetch_list, target_list, options, run_metadata)\u001b[0m\n\u001b[1;32m   1304\u001b[0m           return tf_session.TF_Run(session, options,\n\u001b[1;32m   1305\u001b[0m                                    \u001b[0mfeed_dict\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfetch_list\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtarget_list\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1306\u001b[0;31m                                    status, run_metadata)\n\u001b[0m\u001b[1;32m   1307\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m   1308\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m_prun_fn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msession\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mhandle\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfeed_dict\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfetch_list\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "#字内不上下浮动+扭曲+60颜色\n",
    "model.load_weights(r'E:\\deeplearn\\OCR\\Sample\\model\\weights-51.hdf5')\n",
    "res = model.fit_generator(cc1,\n",
    "                    steps_per_epoch =145750// batch_size,\n",
    "                    epochs = 100,\n",
    "                    validation_data =cc2 ,\n",
    "                    validation_steps = 1003 // batch_size,\n",
    "                    callbacks =[earlystop,checkpoint,tensorboard],\n",
    "                    verbose=1\n",
    "                    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/100\n",
      "open json\n",
      "图片总量 145750\n",
      "2276/2277 [============================>.] - ETA: 0s - loss: 42.2532 - acc: 0.0000e+00open json\n",
      "图片总量 1003\n",
      "2277/2277 [==============================] - 1192s - loss: 42.2516 - acc: 0.0000e+00 - val_loss: 39.5801 - val_acc: 0.0000e+00\n",
      "Epoch 2/100\n",
      "2277/2277 [==============================] - 1190s - loss: 33.8430 - acc: 0.0089 - val_loss: 32.0627 - val_acc: 0.0250\n",
      "Epoch 3/100\n",
      "2277/2277 [==============================] - 1190s - loss: 24.9440 - acc: 0.1228 - val_loss: 26.5708 - val_acc: 0.0865\n",
      "Epoch 4/100\n",
      "2277/2277 [==============================] - 1190s - loss: 20.6413 - acc: 0.1948 - val_loss: 20.0942 - val_acc: 0.1781\n",
      "Epoch 5/100\n",
      "2277/2277 [==============================] - 1190s - loss: 16.7674 - acc: 0.2200 - val_loss: 15.5865 - val_acc: 0.2010\n",
      "Epoch 6/100\n",
      "2277/2277 [==============================] - 1189s - loss: 12.7008 - acc: 0.2290 - val_loss: 12.2085 - val_acc: 0.1760\n",
      "Epoch 7/100\n",
      "2277/2277 [==============================] - 1189s - loss: 9.0045 - acc: 0.2358 - val_loss: 14.6229 - val_acc: 0.2167\n",
      "Epoch 8/100\n",
      "2277/2277 [==============================] - 1189s - loss: 6.1746 - acc: 0.2407 - val_loss: 7.2183 - val_acc: 0.2052\n",
      "Epoch 9/100\n",
      "2277/2277 [==============================] - 1189s - loss: 3.9699 - acc: 0.2483 - val_loss: 4.4610 - val_acc: 0.2521\n",
      "Epoch 10/100\n",
      "2277/2277 [==============================] - 1189s - loss: 3.3758 - acc: 0.2562 - val_loss: 3.8813 - val_acc: 0.2385\n",
      "Epoch 11/100\n",
      "2277/2277 [==============================] - 1188s - loss: 2.0272 - acc: 0.3083 - val_loss: 2.5268 - val_acc: 0.2844\n",
      "Epoch 12/100\n",
      "2277/2277 [==============================] - 1188s - loss: 1.9620 - acc: 0.3503 - val_loss: 5.3504 - val_acc: 0.2115\n",
      "Epoch 13/100\n",
      "2277/2277 [==============================] - 1188s - loss: 1.4824 - acc: 0.4061 - val_loss: 1.6367 - val_acc: 0.4771\n",
      "Epoch 14/100\n",
      "2277/2277 [==============================] - 1188s - loss: 1.2178 - acc: 0.4858 - val_loss: 1.8784 - val_acc: 0.3948\n",
      "Epoch 15/100\n",
      "2277/2277 [==============================] - 1188s - loss: 0.9270 - acc: 0.5670 - val_loss: 1.4191 - val_acc: 0.5604\n",
      "Epoch 16/100\n",
      "2277/2277 [==============================] - 1189s - loss: 0.7183 - acc: 0.6444 - val_loss: 3.1033 - val_acc: 0.3198\n",
      "Epoch 17/100\n",
      "2277/2277 [==============================] - 1189s - loss: 0.5566 - acc: 0.7167 - val_loss: 1.0891 - val_acc: 0.6698\n",
      "Epoch 18/100\n",
      "2277/2277 [==============================] - 1189s - loss: 0.6119 - acc: 0.7159 - val_loss: 1.4789 - val_acc: 0.5344\n",
      "Epoch 19/100\n",
      "2277/2277 [==============================] - 1189s - loss: 0.5565 - acc: 0.7518 - val_loss: 1.3862 - val_acc: 0.5781\n",
      "Epoch 20/100\n",
      "2277/2277 [==============================] - 1189s - loss: 0.7398 - acc: 0.7216 - val_loss: 2.9964 - val_acc: 0.3708\n",
      "Epoch 21/100\n",
      "2277/2277 [==============================] - 1189s - loss: 0.6903 - acc: 0.7264 - val_loss: 0.8712 - val_acc: 0.8323\n",
      "Epoch 22/100\n",
      "2277/2277 [==============================] - 1189s - loss: 1.1729 - acc: 0.7073 - val_loss: 1.0069 - val_acc: 0.7073\n",
      "Epoch 23/100\n",
      "2277/2277 [==============================] - 1188s - loss: 0.4669 - acc: 0.7927 - val_loss: 1.0423 - val_acc: 0.7750\n",
      "Epoch 24/100\n",
      "2277/2277 [==============================] - 1189s - loss: 0.3969 - acc: 0.8293 - val_loss: 1.0589 - val_acc: 0.7917\n",
      "Epoch 25/100\n",
      "2277/2277 [==============================] - 1189s - loss: 0.9760 - acc: 0.7046 - val_loss: 1.0087 - val_acc: 0.8010\n",
      "Epoch 26/100\n",
      "2277/2277 [==============================] - 1190s - loss: 0.3669 - acc: 0.8413 - val_loss: 0.7283 - val_acc: 0.8281\n",
      "Epoch 27/100\n",
      "2277/2277 [==============================] - 1189s - loss: 0.3444 - acc: 0.8529 - val_loss: 0.6071 - val_acc: 0.8865\n",
      "Epoch 28/100\n",
      "2277/2277 [==============================] - 1190s - loss: 1.2916 - acc: 0.6498 - val_loss: 48.9168 - val_acc: 0.0063\n",
      "Epoch 29/100\n",
      "2277/2277 [==============================] - 1190s - loss: 1.5577 - acc: 0.5889 - val_loss: 1.7557 - val_acc: 0.5656\n",
      "Epoch 30/100\n",
      "2277/2277 [==============================] - 1190s - loss: 1.1971 - acc: 0.6252 - val_loss: 2.6575 - val_acc: 0.4198\n",
      "Epoch 31/100\n",
      "2277/2277 [==============================] - 1189s - loss: 2.8463 - acc: 0.4603 - val_loss: 1.4074 - val_acc: 0.6198\n",
      "Epoch 32/100\n",
      "2277/2277 [==============================] - 1189s - loss: 0.8533 - acc: 0.7190 - val_loss: 0.8362 - val_acc: 0.7865\n",
      "Epoch 33/100\n",
      "2277/2277 [==============================] - 1190s - loss: 0.3180 - acc: 0.8618 - val_loss: 0.8860 - val_acc: 0.8510\n",
      "Epoch 34/100\n",
      "2277/2277 [==============================] - 1191s - loss: 0.2225 - acc: 0.9027 - val_loss: 0.6744 - val_acc: 0.9052\n",
      "Epoch 35/100\n",
      "2277/2277 [==============================] - 1190s - loss: 0.2292 - acc: 0.9047 - val_loss: 0.5132 - val_acc: 0.8479\n",
      "Epoch 36/100\n",
      "2277/2277 [==============================] - 1191s - loss: 0.1716 - acc: 0.9296 - val_loss: 0.3561 - val_acc: 0.9010\n",
      "Epoch 37/100\n",
      "2277/2277 [==============================] - 1190s - loss: 0.1784 - acc: 0.9284 - val_loss: 0.7234 - val_acc: 0.8927\n",
      "Epoch 38/100\n",
      "2277/2277 [==============================] - 1189s - loss: 0.1527 - acc: 0.9402 - val_loss: 0.5668 - val_acc: 0.9490\n",
      "Epoch 39/100\n",
      "2277/2277 [==============================] - 1190s - loss: 0.1663 - acc: 0.9372 - val_loss: 0.5672 - val_acc: 0.9250\n",
      "Epoch 40/100\n",
      "2277/2277 [==============================] - 1190s - loss: 0.1366 - acc: 0.9487 - val_loss: 0.6873 - val_acc: 0.9385\n",
      "Epoch 41/100\n",
      "2277/2277 [==============================] - 1189s - loss: 0.1316 - acc: 0.9502 - val_loss: 0.4015 - val_acc: 0.9396\n",
      "Epoch 42/100\n",
      "2277/2277 [==============================] - 1199s - loss: 0.1105 - acc: 0.9585 - val_loss: 0.6870 - val_acc: 0.9198\n",
      "Epoch 43/100\n",
      "2277/2277 [==============================] - 2999s - loss: 0.1284 - acc: 0.9524 - val_loss: 0.3373 - val_acc: 0.9177\n",
      "Epoch 44/100\n",
      "2277/2277 [==============================] - 2240s - loss: 0.1012 - acc: 0.9618 - val_loss: 0.3856 - val_acc: 0.9385\n",
      "Epoch 45/100\n",
      "2277/2277 [==============================] - 1216s - loss: 0.1147 - acc: 0.9581 - val_loss: 0.7114 - val_acc: 0.9396\n",
      "Epoch 46/100\n",
      "2277/2277 [==============================] - 1190s - loss: 0.0979 - acc: 0.9638 - val_loss: 0.5585 - val_acc: 0.9365\n",
      "Epoch 47/100\n",
      "2277/2277 [==============================] - 1191s - loss: 0.1155 - acc: 0.9573 - val_loss: 0.5124 - val_acc: 0.9260\n",
      "Epoch 48/100\n",
      "2277/2277 [==============================] - 1191s - loss: 0.0871 - acc: 0.9693 - val_loss: 0.8996 - val_acc: 0.8583\n",
      "Epoch 49/100\n",
      "2277/2277 [==============================] - 1191s - loss: 0.1009 - acc: 0.9650 - val_loss: 0.5080 - val_acc: 0.9417\n",
      "Epoch 50/100\n",
      "2277/2277 [==============================] - 1552s - loss: 0.0782 - acc: 0.9716 - val_loss: 0.1615 - val_acc: 0.9479\n",
      "Epoch 51/100\n",
      "2277/2277 [==============================] - 2663s - loss: 0.0848 - acc: 0.9697 - val_loss: 0.2192 - val_acc: 0.9740\n",
      "Epoch 52/100\n",
      "2277/2277 [==============================] - 2656s - loss: 0.0868 - acc: 0.9696 - val_loss: 0.2150 - val_acc: 0.9385\n",
      "Epoch 53/100\n",
      "2277/2277 [==============================] - 2667s - loss: 0.0705 - acc: 0.9749 - val_loss: 0.1732 - val_acc: 0.9604\n",
      "Epoch 54/100\n",
      "2277/2277 [==============================] - 2665s - loss: 0.0996 - acc: 0.9657 - val_loss: 0.3687 - val_acc: 0.9406\n",
      "Epoch 55/100\n",
      "2277/2277 [==============================] - 2659s - loss: 0.0708 - acc: 0.9750 - val_loss: 0.1540 - val_acc: 0.9573\n",
      "Epoch 56/100\n",
      "2277/2277 [==============================] - 2662s - loss: 0.0780 - acc: 0.9731 - val_loss: 0.1838 - val_acc: 0.9396\n",
      "Epoch 57/100\n",
      "2277/2277 [==============================] - 2665s - loss: 0.0753 - acc: 0.9737 - val_loss: 0.1513 - val_acc: 0.9646\n",
      "Epoch 58/100\n",
      "2277/2277 [==============================] - 1982s - loss: 0.0570 - acc: 0.9791 - val_loss: 0.5270 - val_acc: 0.9604\n",
      "Epoch 59/100\n",
      "2277/2277 [==============================] - 1456s - loss: 0.0675 - acc: 0.9764 - val_loss: 0.1129 - val_acc: 0.9729\n",
      "Epoch 60/100\n",
      "2277/2277 [==============================] - 1668s - loss: 0.0856 - acc: 0.9714 - val_loss: 0.7182 - val_acc: 0.9542\n",
      "Epoch 61/100\n",
      "2277/2277 [==============================] - 1194s - loss: 0.0537 - acc: 0.9813 - val_loss: 0.5139 - val_acc: 0.9490\n",
      "Epoch 62/100\n",
      "2277/2277 [==============================] - 1190s - loss: 0.0730 - acc: 0.9756 - val_loss: 0.6564 - val_acc: 0.9521\n",
      "Epoch 63/100\n",
      "2277/2277 [==============================] - 1193s - loss: 0.0792 - acc: 0.9744 - val_loss: 0.6379 - val_acc: 0.9208\n",
      "Epoch 64/100\n",
      "2277/2277 [==============================] - 1194s - loss: 0.0898 - acc: 0.9707 - val_loss: 0.6156 - val_acc: 0.9594\n",
      "Epoch 65/100\n",
      " 511/2277 [=====>........................] - ETA: 926s - loss: 0.1099 - acc: 0.9659"
     ]
    }
   ],
   "source": [
    "#字内不上下浮动\n",
    "res = model.fit_generator(cc1,\n",
    "                    steps_per_epoch =145750// batch_size,\n",
    "                    epochs = 100,\n",
    "                    validation_data =cc2 ,\n",
    "                    validation_steps = 1003 // batch_size,\n",
    "                    callbacks =[earlystop,checkpoint,tensorboard],\n",
    "                    verbose=1\n",
    "                    )\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "with open(r'E:\\deeplearn\\OCR\\Sample\\trainnew\\his', 'wb') as file_pi:\n",
    "        pickle.dump(history.history, file_pi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 10/100\n",
      "2276/2277 [============================>.] - ETA: 0s - loss: 6.1203 - acc: 0.2081open json\n",
      "图片总量 1003\n",
      "2277/2277 [==============================] - 1175s - loss: 6.1216 - acc: 0.2081 - val_loss: 19.0974 - val_acc: 0.0979\n",
      "Epoch 11/100\n",
      "2277/2277 [==============================] - 1175s - loss: 2.0049 - acc: 0.3155 - val_loss: 1.1752 - val_acc: 0.4854\n",
      "Epoch 12/100\n",
      "2277/2277 [==============================] - 1174s - loss: 0.6835 - acc: 0.6122 - val_loss: 0.8320 - val_acc: 0.7469\n",
      "Epoch 13/100\n",
      "2277/2277 [==============================] - 1173s - loss: 0.5192 - acc: 0.7387 - val_loss: 0.9210 - val_acc: 0.7635\n",
      "Epoch 14/100\n",
      "2277/2277 [==============================] - 1174s - loss: 0.3913 - acc: 0.8185 - val_loss: 0.6165 - val_acc: 0.8625\n",
      "Epoch 15/100\n",
      "2277/2277 [==============================] - 1174s - loss: 0.2878 - acc: 0.8751 - val_loss: 0.6487 - val_acc: 0.8771\n",
      "Epoch 16/100\n",
      "2277/2277 [==============================] - 1175s - loss: 0.2430 - acc: 0.8975 - val_loss: 0.7223 - val_acc: 0.8792\n",
      "Epoch 17/100\n",
      "2277/2277 [==============================] - 1174s - loss: 0.1870 - acc: 0.9213 - val_loss: 0.7071 - val_acc: 0.8490\n",
      "Epoch 18/100\n",
      "2277/2277 [==============================] - 1174s - loss: 0.1799 - acc: 0.9242 - val_loss: 0.7029 - val_acc: 0.8750\n",
      "Epoch 19/100\n",
      "2277/2277 [==============================] - 1175s - loss: 0.1423 - acc: 0.9421 - val_loss: 0.6549 - val_acc: 0.9313\n",
      "Epoch 20/100\n",
      "2277/2277 [==============================] - 1175s - loss: 0.1494 - acc: 0.9400 - val_loss: 0.6200 - val_acc: 0.8812\n",
      "Epoch 21/100\n",
      "2277/2277 [==============================] - 1175s - loss: 0.1348 - acc: 0.9464 - val_loss: 0.6636 - val_acc: 0.9313\n",
      "Epoch 22/100\n",
      "2277/2277 [==============================] - 1175s - loss: 0.1311 - acc: 0.9484 - val_loss: 0.7251 - val_acc: 0.8490\n",
      "Epoch 23/100\n",
      "2277/2277 [==============================] - 1174s - loss: 0.1294 - acc: 0.9498 - val_loss: 0.4722 - val_acc: 0.9250\n",
      "Epoch 24/100\n",
      "2277/2277 [==============================] - 1174s - loss: 0.0933 - acc: 0.9639 - val_loss: 0.4332 - val_acc: 0.9021\n",
      "Epoch 25/100\n",
      "2277/2277 [==============================] - 1175s - loss: 0.0991 - acc: 0.9628 - val_loss: 0.6637 - val_acc: 0.9323\n",
      "Epoch 26/100\n",
      "2277/2277 [==============================] - 1174s - loss: 0.0868 - acc: 0.9669 - val_loss: 0.6097 - val_acc: 0.8979\n",
      "Epoch 27/100\n",
      "2277/2277 [==============================] - 1174s - loss: 0.1076 - acc: 0.9591 - val_loss: 0.6515 - val_acc: 0.9135\n",
      "Epoch 28/100\n",
      "2277/2277 [==============================] - 1175s - loss: 0.0744 - acc: 0.9725 - val_loss: 0.7833 - val_acc: 0.8896\n",
      "Epoch 29/100\n",
      "2277/2277 [==============================] - 1174s - loss: 0.0866 - acc: 0.9673 - val_loss: 0.5486 - val_acc: 0.9500\n",
      "Epoch 30/100\n",
      "2277/2277 [==============================] - 1174s - loss: 0.0766 - acc: 0.9716 - val_loss: 0.7116 - val_acc: 0.8938\n",
      "Epoch 31/100\n",
      "2277/2277 [==============================] - 1174s - loss: 0.0706 - acc: 0.9736 - val_loss: 0.5864 - val_acc: 0.9427\n",
      "Epoch 32/100\n",
      "2277/2277 [==============================] - 1173s - loss: 0.0654 - acc: 0.9753 - val_loss: 0.4675 - val_acc: 0.9385\n",
      "Epoch 33/100\n",
      "2277/2277 [==============================] - 1173s - loss: 0.0756 - acc: 0.9724 - val_loss: 0.2101 - val_acc: 0.9625\n",
      "Epoch 34/100\n",
      "2277/2277 [==============================] - 1173s - loss: 0.0680 - acc: 0.9754 - val_loss: 0.5675 - val_acc: 0.9490\n",
      "Epoch 35/100\n",
      "2277/2277 [==============================] - 1173s - loss: 0.0665 - acc: 0.9767 - val_loss: 0.5238 - val_acc: 0.9573\n",
      "Epoch 36/100\n",
      "2277/2277 [==============================] - 1174s - loss: 0.0592 - acc: 0.9781 - val_loss: 0.5606 - val_acc: 0.9333\n",
      "Epoch 37/100\n",
      "2277/2277 [==============================] - 1174s - loss: 0.0601 - acc: 0.9776 - val_loss: 0.4504 - val_acc: 0.9646\n",
      "Epoch 38/100\n",
      "2277/2277 [==============================] - 1174s - loss: 0.0924 - acc: 0.9674 - val_loss: 0.3845 - val_acc: 0.9448\n",
      "Epoch 39/100\n",
      "2277/2277 [==============================] - 1174s - loss: 0.0768 - acc: 0.9733 - val_loss: 0.6855 - val_acc: 0.9365\n",
      "Epoch 40/100\n",
      "2277/2277 [==============================] - 1174s - loss: 0.0826 - acc: 0.9705 - val_loss: 0.6044 - val_acc: 0.9708\n",
      "Epoch 41/100\n",
      "2277/2277 [==============================] - 1174s - loss: 0.0644 - acc: 0.9769 - val_loss: 0.3848 - val_acc: 0.9552\n",
      "Epoch 42/100\n",
      "2277/2277 [==============================] - 1176s - loss: 0.0661 - acc: 0.9763 - val_loss: 0.4430 - val_acc: 0.9260\n",
      "Epoch 43/100\n",
      "2277/2277 [==============================] - 1175s - loss: 0.0655 - acc: 0.9769 - val_loss: 0.4695 - val_acc: 0.9333\n",
      "Epoch 44/100\n",
      "2277/2277 [==============================] - 1176s - loss: 0.0546 - acc: 0.9797 - val_loss: 0.8238 - val_acc: 0.8427\n"
     ]
    }
   ],
   "source": [
    "\n",
    "res = model.fit_generator(cc1,\n",
    "                    steps_per_epoch =145750// batch_size,\n",
    "                    epochs = 100,\n",
    "                    validation_data =cc2 ,\n",
    "                    validation_steps = 1003 // batch_size,\n",
    "                    callbacks =[earlystop,checkpoint,tensorboard],\n",
    "                    verbose=1,\n",
    "                    initial_epoch=9\n",
    "                    )\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 47/100\n",
      "2277/2277 [==============================] - 1178s - loss: 0.0607 - acc: 0.9788 - val_loss: 0.3766 - val_acc: 0.9271\n",
      "Epoch 48/100\n",
      "2277/2277 [==============================] - 1180s - loss: 0.0847 - acc: 0.9716 - val_loss: 0.5732 - val_acc: 0.9531\n",
      "Epoch 49/100\n",
      "2277/2277 [==============================] - 1179s - loss: 0.0557 - acc: 0.9804 - val_loss: 0.6628 - val_acc: 0.9646\n",
      "Epoch 50/100\n",
      "2277/2277 [==============================] - 1180s - loss: 0.0543 - acc: 0.9808 - val_loss: 0.3830 - val_acc: 0.9542\n",
      "Epoch 51/100\n",
      "2277/2277 [==============================] - 1182s - loss: 0.0540 - acc: 0.9806 - val_loss: 0.6029 - val_acc: 0.9469\n",
      "Epoch 52/100\n",
      "2277/2277 [==============================] - 1179s - loss: 0.0574 - acc: 0.9801 - val_loss: 0.5402 - val_acc: 0.8781\n",
      "Epoch 53/100\n",
      "2277/2277 [==============================] - 1178s - loss: 0.0658 - acc: 0.9773 - val_loss: 0.2857 - val_acc: 0.9656\n",
      "Epoch 54/100\n",
      "2277/2277 [==============================] - 1178s - loss: 0.0443 - acc: 0.9846 - val_loss: 0.5880 - val_acc: 0.8917\n",
      "Epoch 55/100\n",
      "2277/2277 [==============================] - 1179s - loss: 0.0628 - acc: 0.9774 - val_loss: 0.5438 - val_acc: 0.9635\n",
      "Epoch 56/100\n",
      "2277/2277 [==============================] - 1181s - loss: 0.0501 - acc: 0.9820 - val_loss: 0.6038 - val_acc: 0.9510\n",
      "Epoch 57/100\n",
      "2277/2277 [==============================] - 1182s - loss: 0.0572 - acc: 0.9799 - val_loss: 0.5970 - val_acc: 0.9510\n",
      "Epoch 58/100\n",
      "2277/2277 [==============================] - 1180s - loss: 0.0530 - acc: 0.9818 - val_loss: 0.4549 - val_acc: 0.9625\n",
      "Epoch 59/100\n",
      "2277/2277 [==============================] - 1179s - loss: 0.0522 - acc: 0.9823 - val_loss: 0.6999 - val_acc: 0.9333\n",
      "Epoch 60/100\n",
      "2277/2277 [==============================] - 1178s - loss: 0.0530 - acc: 0.9817 - val_loss: 0.5906 - val_acc: 0.9573\n",
      "Epoch 61/100\n",
      "2277/2277 [==============================] - 1178s - loss: 0.0499 - acc: 0.9820 - val_loss: 0.4906 - val_acc: 0.9625\n",
      "Epoch 62/100\n",
      "2277/2277 [==============================] - 1177s - loss: 0.0600 - acc: 0.9794 - val_loss: 0.4833 - val_acc: 0.9458\n",
      "Epoch 63/100\n",
      "2277/2277 [==============================] - 1176s - loss: 0.0457 - acc: 0.9832 - val_loss: 0.4246 - val_acc: 0.9542\n",
      "Epoch 64/100\n",
      "2277/2277 [==============================] - 1179s - loss: 0.0473 - acc: 0.9833 - val_loss: 0.2392 - val_acc: 0.9740\n",
      "Epoch 65/100\n",
      "2277/2277 [==============================] - 1182s - loss: 0.0552 - acc: 0.9812 - val_loss: 0.7818 - val_acc: 0.9521\n",
      "Epoch 66/100\n",
      "2277/2277 [==============================] - 1174s - loss: 0.0494 - acc: 0.9825 - val_loss: 0.6792 - val_acc: 0.9625\n",
      "Epoch 67/100\n",
      "2277/2277 [==============================] - 1178s - loss: 0.0502 - acc: 0.9823 - val_loss: 0.5949 - val_acc: 0.9437\n",
      "Epoch 68/100\n",
      " 903/2277 [==========>...................] - ETA: 715s - loss: 0.0401 - acc: 0.9857"
     ]
    }
   ],
   "source": [
    "#字内上下浮动\n",
    "res = model.fit_generator(cc1,\n",
    "                    steps_per_epoch =145750// batch_size,\n",
    "                    epochs = 100,\n",
    "                    validation_data =cc2 ,\n",
    "                    validation_steps = 1003 // batch_size,\n",
    "                    callbacks =[earlystop,checkpoint],\n",
    "                    verbose=1,\n",
    "                    initial_epoch=46\n",
    "                    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 3/3\n",
      " 128/1125 [==>...........................] - ETA: 631s - loss: 4.8391 - acc: 0.1152"
     ]
    }
   ],
   "source": [
    "model.optimizer.lr =0.000000000001\n",
    "model.fit_generator(cc1,\n",
    "                    steps_per_epoch =36000// batch_size,\n",
    "                    epochs = 3,\n",
    "                    validation_data =cc2 ,\n",
    "                    validation_steps = 2000 // batch_size,\n",
    "                    callbacks =[tensorboard,earlystop,checkpoint],\n",
    "                    verbose=1,\n",
    "                    initial_epoch=2\n",
    "                    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
